package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class DVD here.
 * 
 * @author Ronnie Hemmingsen 
 * @version (-2012)
 */
public class DVD
{
    // instance variables - replace the example below with your own
    private ArrayList<Copy> copies;
    private long id;
    private String title;
    private String director;
    private int releaseDate;

    /**
     * Constructor for objects of class DVD
     */
    public DVD(long id, String title, String director, int releaseDate)
    {
        this.id = id;
        this.title = title;
        this.director = director;
        this.releaseDate = releaseDate;
        this.copies = new ArrayList<Copy>();
    
    }
    
    //Getters and Setters
    public long getId()
    {
        return id;
    }
    public String getTitle()
    {
        return title;
    }
    public String getDirector()
    {
        return director;
    }
    public int getReleaseDate()
    {
        return releaseDate;
    }
    public ArrayList<Copy> getCopies()
    {
        return copies;
    }
    
    public void setId(long id)
    {
        this.id = id;
    }
    public void setTitle(String title)
    {
        this.title = title;
    }
    public void setDirector(String director)
    {
        this.director = director;
    }
    public void setReleaseDate(int releaseDate)
    {
        this.releaseDate = releaseDate;
    }
    
    //
    /**
     * Adds a copy to the ArrayList copies
     * @param Copy The copy to be inserted in the ArrayList
     */
    public void addCopy(Copy c)
    {
        copies.add(c);
    }
    
    /**
     * Deletes a copy from the ArrayList copies
     * @param int The id of the copy that is to be deleted
     */
    public void deleteCopy(int serialNo)
    {
        int index = 0;
        boolean found = false;
        
        while (index < copies.size() && !found) {
            Copy c = copies.get(index);
            
            if (c.getSerialNo() == serialNo) {
            found = true;
            copies.remove(index);
            }
        index++;
        }
    }
    
    /**
     * Locates a copy in the ArrayList based on the serial number
     * @param int The serialNo of the wanted copy
     * @return Copy The copy found in the ArrayList
     */
    public Copy getCopy(int serialNo)
    {
        int index = 0;
        
        while (index < copies.size()) {
            Copy c = copies.get(index);
            
            if (c.getSerialNo() == serialNo){
                return c;
            }
            index++;
        }
       return null;
    }
    
    /**
     * Updates the information stored in a copu. If a field has been left empty, this field will not be updated
     * @param int The serial number of the copy
     * @param int The date on which the copy was aquired.
     * @param int The price of the copy
     */
    public void updateCopy(int serialNo, int accDate, int accPrice)
    {
        Copy c = getCopy(serialNo);
        
        if(serialNo != 0) { c.setSerialNo(serialNo);}
        if(accDate != 0) { c.setAccDate(accDate);}
        if(accPrice != 0) { c.setAccPrice(accPrice);}
    }

    public Copy getFirstCopy()
    {        
        Copy c = copies.get(0);
        return c;
    }
        
            
}
